// 引入接口文件
import {getDetail,create} from './biddingServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';

// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  id: '',
  detailInfo: {},
  formData: {},
  biz: {
    queryForm: {}
  },
  modalObj:{
    visible:false,
  },

}

// 声明module
const tmpModule = {
  // namespace: 'productListModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
          // 执行业务操作
          dispatch({type: 'getDetail', payload: {}}) ;// end dispatch

        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    // 详情
    *getDetail({payload }, { put,select,call}) {
      try {
        let { id,detailInfo,formData} = yield select(state => state[tmpModule.namespace])
        const {success,msg,data={}} = yield call(getDetail, {id:id||payload.id });
        console.log('数据',success,msg,data,payload);
        if(success){
          detailInfo=data
          formData.price=(detailInfo.currentMaxPrice>detailInfo.initPrice?detailInfo.currentMaxPrice:detailInfo.initPrice)||0
          // formData.price=formData.price+(detailInfo.grads||0)
        }
        yield put({ type: 'updateStore', payload: { detailInfo,...payload}});
      } catch (e) {
        console.log(e)
      }

    },
    // 详情
    *create({payload }, { put,select,call}) {
      try {
        let { id,detailInfo,formData,biz} = yield select(state => state[tmpModule.namespace])
        let params={...formData,seckillProductId:id}
        yield put({ type: 'updateStore', payload: { loading: true,btnLoading: true,  } })
        const {success,msg,data={}} = yield call(create, params);
        yield put({ type: 'updateStore', payload: { loading: false,btnLoading: false,  } })
        if(!success){
          return false
        }
        console.log('数据',success,msg,data,payload);
        yield put({ type: 'getDetail', payload: {}});
        message.success('操作成功',()=>{
          biz.refreshListDatas(true);
        })
      } catch (e) {
        console.log(e)
      }

    },
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
